﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using System.Text;  //StringBuilder所在的命名空间
using IoT3Week1.EfCore;
using IoT3Week1.Domain;
using IoT3Week1.App;

namespace IoT3Week1.WebApi.Controllers
{
    [Route("api/[controller]/[action]")] //配置一下路由
    [ApiController]
    public class UserController : ControllerBase
    {
        //字段与属性有什么区别？
        //定义一个私有字段 （readonly关键字的作用是 只读）
        private readonly IUserAppService _userAppService;

        public UserController(IUserAppService userAppService)  //依赖注入
        {
            _userAppService = userAppService;
        }

        [HttpGet]
        public IActionResult GetUserList(string name, string pass)
        {
            List<User> list = _userAppService.GetUsers(name, pass);
            return Ok(list);
        }

        [HttpPost]
        public IActionResult Add(User user)
        {
            int i = _userAppService.Add(user);

            var res = new { msg = i > 0 ? "成功" : "失败", result = i }; //匿名类型

            return Ok(res);

        }
    }
}
